$(function () {
    //全局定义页数与总条数
    let markId = getUrlString('markid');
    let type = getUrlString('type');
    let page = -1,communityPage = -1;
    let communitymRefresh, communityBoolean = true;
    let totalSize = 0,communityTotalSize = 0;
    //获取三大类的未读数
    function notAllCount() {
        let data = {
            'markid':markId,
        };
        getData('get','/niu/apiv3/noti_allcount.php',data,function (data) {
            console.log(data);
            if(data.data.gonggaocount === 0){
                $('#gonggaocount').hide();
            }else {
                $('#gonggaocount').css('display','inline-block').html(data.data.gonggaocount);
            }
            if(data.data.communitycount === 0){
                $('#communitycount').hide();
            }else {
                $('#communitycount').css('display','inline-block').html(data.data.communitycount);
            }
        })
    }
    notAllCount();
    //获取消息通知分类
    function getCateCount() {
        let data = {
            'markid':markId,
        };
        getData('get','/niu/apiv3/noti_getcatecount.php',data,function (data) {
            console.log(data);
            $(data.data).each(function () {
                if(this.type === '0'){

                    if(this.unread === 0){
                        $('#TZCount').hide();
                    }else {
                        $('#TZCount').css('display','inline-block').html(this.unread);
                    }

                }else if (this.type === '1') {

                    if(this.unread === 0){
                        $('#DDZTCount').hide();
                    }else {
                        $('#DDZTCount').css('display','inline-block').html(this.unread);
                    }

                }else if (this.type === '2') {

                    if(this.unread === 0){
                        $('#SPZDCount').hide();
                    }else {
                        $('#SPZDCount').css('display','inline-block').html(this.unread);
                    }

                }else if (this.type === '3') {

                    if(this.unread === 0){
                        $('#SPDQCount').hide();
                    }else {
                        $('#SPDQCount').css('display','inline-block').html(this.unread);
                    }

                }else if (this.type === '4') {

                    if(this.unread === 0){
                        $('#QBDZCount').hide();
                    }else {
                        $('#QBDZCount').css('display','inline-block').html(this.unread);
                    }

                }else if (this.type === '5') {

                    if(this.unread === 0){
                        $('#JLBCCount').hide();
                    }else {
                        $('#JLBCCount').css('display','inline-block').html(this.unread);
                    }

                }
            })
        })
    }
    getCateCount();

    //全部标记已读
    $('#allRead').click(function () {
        let data = {
            'markid':markId
        };
        getData('post','/niu/apiv3/noti_allcallback.php',data,function (data) {
            console.log(data);
            if(data.resultcode === '0'){
                window.location.reload();
            }else {
                laymsg(data.msg)
            }
        })
    });

    //调往分类列表
    $('.typeList li').click(function () {
        let typeid = $(this).index();
        $(this).find('.listCount').hide();
        window.location.href = 'dancool://fullweb='+urlView+'messagesV2/messageList.html?typeid='+typeid
    });

    //tab切换
    if(type === '0'){
        $('.indexType .left .leftLI').removeClass('active');
        $(this).find('.leftLI').addClass('active');
        $('.informContent,.SQContent').hide();
        $('.messageContent,.allRead').show();
    }else if(type === '1'){
        $('.indexType .left .leftLI').removeClass('active');
        $(this).find('.leftLI').addClass('active');
        $('.informContent,.messageContent,.allRead').hide();
        $('.SQContent').show();
        getData('post','/niu/apiv3/noti_communitycallback.php',{'markid':markId},function (data) {
            console.log(data)
        });
        if(communityBoolean){
            //社区加载
            communitymRefresh = new MiniRefresh({
                container: '#communitymRefresh', //这里是插件的容器ID
                down: {
                    isAuto: false,
                    callback: function () {
                        // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                        $('#mailList').empty();//清空容器
                        communityTotalSize = 0;
                        communityPage = -1;
                        communitymRefresh.endDownLoading();
                    }
                }
                ,
                up: {
                    isAuto: true,
                    callback: function () {
                        // 向上拉后的回调事件
                        communityPage += 1;
                        getSQList(communityPage);
                    }
                }
            });
        }
        communityBoolean = false;
    }else if(type === '2'){
        $('.indexType .left .leftLI').removeClass('active');
        $(this).find('.leftLI').addClass('active');
        $('.messageContent,.SQContent,.allRead').hide();
        $('.informContent').show();
    }

    //社区列表
    function getSQList(page) {
        let data = {
            'markid':markId,
            'page':page
        };
        getData('get','/niu/apiv3/noti_communitylist.php',data,function (data) {
            console.log(data);
            let append = '';
            if (data.datacount === 0){
                $('.nullContent').show()
            } else{
                $('.nullContent').hide();
                $(data.data).each(function () {
                    append+='<li>' +
                        '<input type="hidden" class="url" value="'+this.url+'">'+
                        '                <div class="right">' +
                        '                    <p>' +
                        '                        <span>'+this.title+'</span>' +
                        '                        <span class="time">'+subTime(this.time)+'</span>' +
                        '                    </p>' +
                        '                    <p class="listContent">'+this.intro+'</p>' +
                        '                </div>' +
                        '            </li>'
                });
                $('#mailList').append(append);
                if (page === 0){
                    mRefresh.scrollTo('5','30');
                }

                $('#mailList li').click(function () {
                    let url = $(this).find('.url').val();
                    window.location.href = url
                });

                //总条数
                communityTotalSize = communityTotalSize + 10;
                if (communityTotalSize > data.datacount)
                    communitymRefresh.endUpLoading(true);
                else{
                    communitymRefresh.endUpLoading(false);
                }
            }
        })
    }

    let mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#informList').empty();//清空容器
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                getSLList(page);
            }
        }
    });

    //私信列表
    function getSLList(page) {
        let data = {
            'markid':markId,
            'page':page
        };
        getData('get','/niu/apiv3/mail_getlistv1.php',data,function (data) {
            console.log(data);
            let append = '';
            $(data.data).each(function () {
                if(this.isme === '0'){
                    append+='<li>' +
                        '<input type="hidden" class="toid" value="'+this.fromid+'">'+
                        '<input type="hidden" class="tonick" value="'+this.fromnick+'">'+
                        '                <div class="userFace">' +
                        '                    <img src="'+this.fromface+'" alt="">' +
                        '                </div>' +
                        '                <div class="right">' +
                        '                    <p>' +
                        '                        <span>'+this.fromnick+'</span>' +
                        '                        <span class="time">'+subTime(this.time)+'</span>' +
                        '                    </p>' +
                        '                    <p class="listContent">'+this.intro+'</p>' +
                        '                </div>' +
                        '            </li>'
                }else if(this.isme === '1'){
                    append+='<li>' +
                        '<input type="hidden" class="toid" value="'+this.toid+'">'+
                        '<input type="hidden" class="tonick" value="'+this.tonick+'">'+
                        '                <div class="userFace">' +
                        '                    <img src="'+this.toface+'" alt="">' +
                        '                </div>' +
                        '                <div class="right">' +
                        '                    <p>' +
                        '                        <span>'+this.tonick+'</span>' +
                        '                        <span class="time">'+subTime(this.time)+'</span>' +
                        '                    </p>' +
                        '                    <p class="listContent">'+this.intro+'</p>' +
                        '                </div>' +
                        '            </li>'
                }

            });
            $('#informList').append(append);
            if (page === 0){
                mRefresh.scrollTo('5','30');
            }

            $('#informList li').click(function () {
                let userId = $(this).find('.toid').val();
                let nick = $(this).find('.tonick').val();
                window.location.href = 'dancool://talk='+userId+';'+nick
            });

            //总条数
            totalSize = totalSize + 10;
            if (totalSize > data.datacount)
                mRefresh.endUpLoading(true);
            else{
                mRefresh.endUpLoading(false);
            }
        })
    }

    $('#goBack').click(function () {
        nativeCommand("dancool://activity=fin")
    });
});